<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04猜数字</title>
</head>
<body>
<h3>已生成一个1~100之间随机整数:</h3>
<input type="text" placeholder="请输入你猜的数字">
<button onclick="guess()">点我验证</button>
<h3>小提示: <span></span> </h3>

<script>
    //1.生成随机数 [0,100)+1=[1,101)
    let r = parseInt(Math.random()*100)+1;
    console.log('打个小抄:'+r);
    //定义变量用来保存猜的次数
    let count = 0;
    function guess(){
        let n = document.querySelector('input').value;
        if(isNaN(n)){
            alert('请输入数字!');
            return;
        }
        //获取用来显示结果的span元素
        let spanE = document.querySelector('span');
        //每猜一次,次数+1
        count++;
        if(n>r){
            spanE.innerHTML = '猜大了';
        }else if(n<r){
            spanE.innerHTML = '猜小了';
        }else{
            spanE.innerHTML = '恭喜你用了'+count+'次就猜对了!';
        }
    }
</script>

</body>
</html>